import axios from "axios"
import { nanoid } from "nanoid"
export default {
    namespaced:true,
    actions:{
        set_tableData(context,book){
            console.log(book)
            axios.get("/api/setBook",{
                params:{
                    id:book.index,
                    name:book.book.name,
                    author:book.book.author,
                    price:book.book.price,
                    content:book.book.content,
                }
            }).then(
                response=> {
                     console.log(response)
                     context.commit('SET_TABLEDATA',book)
                },
                err=> {
                  console.log(err)
                  alert("数据库未连接，修改失败！")
                }
            )
            context.commit('SET_TABLEDATA',book)
        },
        delete_tableData(context,index){
            //axios.get
            axios.get("/api/DeleteBook",{
                params:{
                    id:index.row.id
                }
            }).then(
                response=> {
                     console.log(response)
                     context.commit('DELETE_TABLEDATA',index)
                },
                err=> {
                  console.log(err)
                  alert("数据库未连接，删除失败！")
                }
            )
            
        }, 
        add_tableData(context,book){
            console.log(context,book)
            axios.get("/api/AddBook",{
                params:{
                    id:book.id,
                    name:book.name,
                    author:book.author,
                    price:book.price,
                    content:book.content
                }
            }).then(
                response=> {
                     console.log(response)
                     context.commit('ADD_TABLEDATA',book)
                },
                err=> {
                  console.log(err)
                  alert("添加失败！")
                }
            )
            
        },
        update_tableData(context){
            axios.get('/api/UpdateBook').then(
                response=> {
                    // console.log(response.data)
                    let book = response.data.data
                    context.commit('UPDATE_TABLEDATA',book)
                    // console.log(book)
                },
                err=> {
                  console.log(err)
                }   
              )
            
        }
    },
    mutations:{
        SET_TABLEDATA(state,book){
            state.tableData[book.index] = book.book
        },
        DELETE_TABLEDATA(state,index){
            state.tableData = state.tableData.filter(item=>item.id != index.row.id)
        },
        ADD_TABLEDATA(state,book){
            state.tableData.push(book)
        },
        UPDATE_TABLEDATA(state,book){
            state.tableData = book
        }
    },
    state:{
        tableData: [{
            id: '',
            name: '',
            author: '',
            price: '',
            content:""
          },
          ]
    }
}